# 创建时间：2025/5/4 星期日 21:41
# 创建人：李德才


""" 使用元类的创建方式将参数全部转为大写 """


def upper_attr(future_class_name, future_class_parents, future_class_attr):
    new_attr = {}
    for name, value in future_class_attr.items():
        if not name.startswith('__'):
            new_attr[name.upper()] = value
    return type(future_class_name, future_class_parents, new_attr)


class Foo(object, metaclass=upper_attr):
    bar = 'bip'


print(hasattr(Foo, 'bar'))  # False
print(hasattr(Foo, 'BAR'))  # True
foo = Foo()
print(foo.BAR)  # 'bip'

""" 使用类的方式实现元类的创建方式将参数全部转为大写 """


class UpperAttrMetaClass(type):
    def __new__(cls, future_class_name, future_class_parents, future_class_attr):
        new_attr = {}
        for name, value in future_class_attr.items():
            if not name.startswith('__'):
                new_attr[name.upper()] = value
        return type(future_class_name, future_class_parents, new_attr)


class Foo(object, metaclass=UpperAttrMetaClass):
    bar = 'bip'


print(hasattr(Foo, 'bar'))  # False
print(hasattr(Foo, 'BAR'))  # True
foo = Foo()
